/*
 Cogumelo v0.1 - INNOTO
Copyright (C) 2010 Innoto Gestión para el Desarrollo Social S.L. <mapinfo@innoto.es>

This program is free software; you can redistribute it and/or
modify it under the2009 terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
USA.
*/

//
// submit Form function (POST with ajax)
//
//  @finallist: if true shows a final list of errors into a div
//
  var formID = null;
  var formBtnText = null;
  var formOKfunction = null;
  
 
 function submitForm(ID, submitOK, finallist)
  {	
  	formID = ID;
	formOKfunction = submitOK;
	

	new $.ajax({
		  url: $('#'+formID).attr('action'),
		  type: 'POST',
		  dataType: 'json',
		  data: $('#'+formID).serialize(),
		  beforeSend: function(){
				// disable form
				disableForm(formID, true);
	  	  },
		  success: function(transport){
				var validationResult = transport;

				// check if json is all NULL
				if(!validationResult)
				{
					alert("critical error");
					return false;
				}
				// clear all alerts on form
				clearFormAlerts(formID);	

				// show form validation
				if(validationResult.errorlist)
					showFormValidation(validationResult, finallist);
				else{
					disableForm(formID, false);
					formOKfunction(transport);
				}
				// enable form
				disableForm(formID, false);
				
				}
	});					
	
    return false;
  }
  
//
//	Aplicates validation on form
//
function showFormValidation(validationResult, finallist)
{

	// show error msg
	$('#'+validationResult.key +'Error').hide();


	if ($('#'+validationResult.key +'Error'))	
		$('#'+validationResult.key +'Error').html(validationResult.msg)

	if($('#'+validationResult.key +'Error'))
		$('#'+validationResult.key +'Error').show();

	// close waiting dialog
	if($('#'+formID + 'Loading'))
		$('#'+formID + 'Loading').removeClass('isLoading');

	
	// add errors to inputs
	if(validationResult.errorlist != 1) {
		$.each(validationResult.errorlist, function(index, s){
			$('#'+this.id).addClass('alertERROR');
			// show error globes in the inputs if content exist and final list is disabled
			if(s.msg != false && finallist != true) {
				var offset = $('#'+this.id).offset();
				var inputWidth = $('#'+this.id).innerWidth();
				
				
				$("body").append("<div id='"+ this.id+"ErrorMsg" +"' class='submiterrorglobe' style='top:"+offset.top+"px;left:"+ ( inputWidth+offset.left)+ "px;position:absolute;display:none;'><div class='submiterrorglobetext'>"+s.msg+"</div></div>")
				
				$("#"+ this.id+"ErrorMsg").fadeIn('slow');
			}
			
			
		});
	}
	
	if(finallist == true)
	{
		if(validationResult.errorlist != 1) {
			
			var errorText = "<ul>";
			$.each(validationResult.errorlist, function(index, s){
				if (s['msg'] != false)
					errorText += '<li>' + s['msg']+'</li>';
			});
			errorText += "</ul>";
			

			
			if($('#'+validationResult.key +'Sumary'))
				$('#'+validationResult.key +'Sumary').show();
					
			if($('#'+validationResult.key +'ErrorList'))
				$('#'+validationResult.key +'ErrorList').html(errorText);}
	}
}
 

/*
//
//	implode checklist
//
function implodeChecklist(checklistDIV, inputRes)
{
	$('#'+inputRes).val('');
	first=true;
	
	$('#'+checklistDIV+' input').each( function(index, e){
		if (e.checked) {
			if(first==false) $(inputRes).value += ',';
			$('#'+inputRes).value += e.name;
			first = false;
		}

	}.bind(this));
}
*/

//
//	Disable Form
//
function disableForm(formID, isdisable)
{
	$('#'+formID+'Container input').each(function(index, s) {
  		$(s).attr('disabled', isdisable);
	});	
}

//
//	Clear data Alerts
//
function clearFormAlerts(formID){

	if($('#'+formID+'Sumary' ))
		$('#'+formID+'Sumary' ).hide();
	
	$('#'+formID+'Container .alertERROR').each(function(i, e) {
  		$(e).removeClass('alertERROR');
	});
	
	$('.submiterrorglobe').each(function(i, e) {
  		$(e).remove();
	});
}
